<?php
#通知消息表，含添加好友申请
class NoticeModel extends PdoModel {
    protected $_name = 'notice';

    function __construct(){
        parent::__construct();
    }

    #添加
    function addData($data){
        if(empty($data)){
            return false;
        }
        return $this->insert($data);
    }

    #删除
    function deleteData($id = 0){
        if(empty($id)){
            return false;
        }
        $row = $this->find("id = {$id}");
        if (empty($row)) {
            return false;
        }
        return $this->delete("id = {$id}");
    }

    #更新
    function updateDataById($data,$id){
        return $this->update($data,"id = {$id}");
    }

    #更新非邀请好友消息
    function updateIsnewByUid($data,$uid){
        return $this->update($data,"uid = {$uid} and type!=2"); //type=2表示添加好友
    }

    function setAllNewNotificationRead($uid){
        $sql = "update {$this->_name} set isnew = 1 where uid = {$uid} and isnew = 0";
        return $this->execSql($sql);
    }

    function getDataById($id){
        if(empty($id)){
            return array();
        }

        $sql = " select * from {$this->_name} where id={$id} limit 1";
        $result = $this->fetchRow($sql);

        return $result;
    }

    #添加约会邀请的消息
    function dateInvite($uid, $remark){
        $notice_date = array(
            'uid' => $uid,
            'title' => "约会邀请",
            'type' => "1",
            'isnew' => 1,
            'friend_id' => 0,
            'note' => "您的好友".$remark."邀请您加入约会，您可以在“我关心的人”中的“位置约定”查看。",
            'created_at' => time(),
        );
        $this->addData($notice_date);
    }

    #基本表信息
    function getListByAdmin($page = 1, $page_size =  20, $condition = array()){
        $sql = " select * from {$this->_name} where 1 ";
        if(!empty($condition['uid'])) {
            $sql .= " and (uid = {$condition['uid']} or uid=0)";
        }
        if(isset($condition['isnew'])) {
            $sql .= " and isnew = {$condition['isnew']} ";
        }

        if(isset($condition['type'])) {
            $sql .= " and type in( {$condition['type']} ) ";
        }

        $sql .= " order by created_at desc ";

        $start = ($page -1 ) * $page_size;
        $sql .= " limit {$start}, {$page_size}";

        $data = $this->fetchAll($sql);
        return $data;
    }

    #基本表信息
    function getListByAdminCount($condition = array()){
        $sql = " select count(*) as num from {$this->_name} where 1 ";
        if(!empty($condition['uid'])) {
            $sql .= " and uid={$condition['uid']} ";
        }
        if(isset($condition['isnew'])) {
            $sql .= " and isnew = {$condition['isnew']} ";
        }
        if(isset($condition['type'])) {
            $sql .= " and type in( {$condition['type']} ) ";
        }

        if(isset($condition['page_size'])) {
            $sql .= " limit {$condition['page_size']} ";
        }

        $result = $this->fetchRow($sql);
        $num = 0;
        if(!empty($result['num'])) {
            $num = $result['num'];
        }
        return $num;
    }


}
